package com.ctbu.sbdemol.controller;

import com.ctbu.sbdemol.entity.Admin;
import com.ctbu.sbdemol.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * 实现对数据库的增删改查
 */
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 返回数据库所有管理员
     * @return
     */
    @RequestMapping(value = "/GetAdminAll")
   public List<Admin>GetAdminAll(){

        return adminService.findAll();
    }

    /**
     * 利用用户名进行查询
     * @param
     * @return
     */
    /*@RequestMapping(value = "/getByName")
    public List<Admin>GetAdminByName(@RequestParam("name")String Name)
    {
        return  adminService.findByName(Name);
    }*/


    @RequestMapping(value = "/newAdmin")
    public String NewAdmin(){
        Admin admin=new Admin();
        admin.setName("我是");
        admin.setPassword("您看不到我");
        adminService.Save(admin);
        return    "success";
    }

    /**
     * 根据Id查找
     * @param id
     * @return
     */
    @RequestMapping(value = "/getAdminById")
    public Optional<Admin>getAdminById(Integer id)
    {
        return adminService.findById(id);
    }

    /**
     * 根据名字查找
     * @param name
     * @return
     */
    @RequestMapping(value = "/getAdminByName")
    public List<Admin>getAdminByName(String name)
    {
        return adminService.findByName(name);
    }



    @RequestMapping(value = "/addNew")
    public String save(){

        Admin admin=new Admin();
        admin.setName("王五");
        admin.setPassword("123456");
        adminService.Save(admin);
        return  "success";
    }

    /**
     * 更新操作1
     * @return
     */
    @RequestMapping(value = "/update")
    public  String  update(){
       List<Admin>adminList=adminService.findByName("张三");
       if(adminList.size()>0){
           Admin admin=adminList.get(0);
           admin.setPassword("新密码");
           adminService.update(admin);
       }
       return  "success";
    }
    /**
     * 更新操作2
     */
    @RequestMapping(value = "/update2")
    public String update2(@RequestParam("id")Integer id){
        Optional<Admin>adminOptional=adminService.findById(id);
        if(adminOptional.isPresent()) //存在的情况下
        {
            Admin admin=adminOptional.get();
            admin.setName("更新的名字");
            admin.setPassword("更新的秘密");
            adminService.save(admin);
        }
        return "success";
    }
    /**
     * 删除操作
     * @param id
     * @return
     */
    //删除操作
    @RequestMapping(value = "/delete")
    public String delete(@RequestParam("id")Integer id){
        Optional<Admin>adminList=adminService.findById(id);
        if(adminList.isPresent())
        {
            Admin admin1=new Admin();
            admin1.setId(id);
            adminService.delete(admin1);
        }
        return "success";
    }
}
